package com.t235.gc.goods.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.t235.gc.AjaxResult;
import com.t235.gc.entity.GoodsCategory;
import com.t235.gc.goods.service.GoodsCategoryService;
import com.t235.gc.goods.service.GoodsService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@RestController
@CrossOrigin
@RequestMapping("/api/goods-category/admin")
public class GoodsCategoryController {
    @Autowired
    GoodsCategoryService goodsCategoryService;
    @Autowired
    GoodsService goodsService;

    @GetMapping("/getAllGoodsCategoryList")
    public AjaxResult getAllGoodsCategoryList(GoodsCategory goodsCategory, Integer page, Integer limit){
        //查询商品类型所有数据
        if(page!=null){
            PageHelper.startPage(page,limit);
            List<GoodsCategory> goodsList =goodsCategoryService.FindByAll(goodsCategory);
            PageInfo<GoodsCategory> pages=new PageInfo<>(goodsList);
            List<GoodsCategory> list = pages.getList();
            long total = pages.getTotal();
            return AjaxResult.success(list,total);

        }else{
            List<GoodsCategory> goodsCategories = goodsCategoryService.FindByAll(goodsCategory);

            return AjaxResult.success(goodsCategories);
        }
    }
    @PostMapping("/saveOrUpdateGoodsCategory")
    public AjaxResult saveOrUpdateGoodsCategoryAdd(GoodsCategory goodsCategory){
        //添加商品类型
       // Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //SysManager sysManager = (SysManager)authentication.getPrincipal();
        //goodsCategory.setCname(sysManager.getUsername());
        if(goodsCategoryService.insertSelective(goodsCategory)>0){
            return AjaxResult.success("添加成功");
        }else {
            return AjaxResult.error("添加失败");
        }

    }
    @PutMapping("/saveOrUpdateGoodsCategory")
    public AjaxResult saveOrUpdateGoodsCategory(GoodsCategory goodsCategory){
        //修改商品类型
        if(goodsCategoryService.updateByPrimaryKeySelective(goodsCategory)>0){
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.error("修改失败");
        }
    }
    @DeleteMapping("/delGoodsCategory")
    public AjaxResult delGoodsCategory(Long goodscategoryid){
        //删除商品类型
        if(goodsCategoryService.deleteByPrimaryKey(goodscategoryid)>0){
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.error("删除失败");
        }
    }

}
